package homework;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.StringTokenizer;

public class Counter implements ActionListener {
    private  JFrame frame = new JFrame("简易计算器");//写标题；
    //private  Label label = new Label();
    private JPanel panel = new JPanel();//新建面板；
    private JPanel borderLayout = new JPanel();
    private JPanel gridPanel = new JPanel();
    private JTextField text = new JTextField(40);
    private double result=0.0;
    private JButton[] buttons = new JButton[3];
    private JButton[] buttons1 = new JButton[20];

    private String[] name = {
            "BackSpace", "CE", "C"
    };

    private String[] name1 = {
            "7", "8", "9", "/", "sqrt",
            "4", "5", "6", "*", "%",
            "1", "2", "3", "-", "1/x",
            "0", "+/-", ".", "+", "="
    };

    public Counter() {
        inti();
    }

    private void inti()
    {
        frame.addWindowListener(new WindowAdapter() {//设置关闭窗口；
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });
        panel.add(text);//设置文本框；

        text.setHorizontalAlignment(SwingConstants.RIGHT);
        text.setFont(new Font("楷体", Font.PLAIN, 25));
        borderLayout.setLayout(new GridLayout(1, 1, 2, 2));//并设置为网格布局；
        gridPanel.setLayout(new GridLayout(4, 5, 3, 3));//并设置为网格布局；
        //循环添加按钮；
        for (int i = 0; i < name.length; i++) {
            buttons[i] = new JButton(name[i]);
            borderLayout.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
        //循环添加按钮；
        for (int i = 0; i < name1.length; i++) {
            buttons1[i] = new JButton(name1[i]);
            buttons1[i].addActionListener(this);
            gridPanel.add(buttons1[i]);
        }

        frame.add(panel, BorderLayout.NORTH);//把面板添加至frame框架中，置于最上端；
        frame.add(borderLayout, BorderLayout.CENTER);//把面板添加至frame框架中，置于中端；
        frame.add(gridPanel, BorderLayout.SOUTH);//把面板添加至frame框架中，置于最下端；
        frame.setBounds(800, 200, 500, 300);
        frame.pack();
        frame.setVisible(true);//设置可见；
    }
    public static void main(String[] args) {
        new homework.Counter();
    }

    public void actionPerformed(ActionEvent e) {
        Object object=e.getSource();//获得事件按钮对象；
        String str=e.getActionCommand();
        if("0123456789.+-*/".indexOf(str)>=0)
        {
            mumber(str);
        }else if(str==name[0]){//BackSpace;
            BackSpace();
        }else if(str==name[1]){//CE;
            CE();
        }else if(str==name[2]){//C;
            C(str);
        }else if(str==name1[19]){
            equal();
        }
        else if(str==name1[4])
        {
            sqrt();
        }else if(str==name1[14]){
            daoShu();
        }else if(str==name1[9]){
            prencent();
        }else{

        }
    }
    Boolean flag = true;
    void mumber(String str){
        this.text.setText(text.getText()+str);
        System.out.println(str);
    }
    void BackSpace(){
        String str1=text.getText();
        str1=str1.substring(0,str1.length()-1);
        text.setText(str1);
    }
    void CE(){
        text.setText("0");
    }
    void C(String str){
        str="";
        text.setText("");
    }
    void equal() {//对等号进行操作；加减乘除；
        double d1 = 0.0;
        double d2 = 0.0;
        text.setText(text.getText() + buttons1[19].getText());
        if (text.getText().contains("+")) {//做加法运算
            StringTokenizer st1 = new StringTokenizer(text.getText(), "+");
            d1 = Double.parseDouble(st1.nextToken());
            while (st1.hasMoreTokens()) {
                StringTokenizer st2 = new StringTokenizer(st1.nextToken(), "=");
                d2 = Double.parseDouble(st2.nextToken());
            }
            text.setText(text.getText() + " " + (d1 + d2));
        } else if (text.getText().contains("-")) {//做减法运算
            StringTokenizer st1 = new StringTokenizer(text.getText(), "-");
            d1 = Double.parseDouble(st1.nextToken());
            while (st1.hasMoreTokens()) {
                StringTokenizer st2 = new StringTokenizer(st1.nextToken(), "=");
                d2 = Double.parseDouble(st2.nextToken());
            }
            text.setText(text.getText() + " " + (d1 - d2));
        } else if (text.getText().contains("*")) {//做乘法运算
            StringTokenizer st1 = new StringTokenizer(text.getText(), "*");
            d1 = Double.parseDouble(st1.nextToken());
            while (st1.hasMoreTokens()) {
                StringTokenizer st2 = new StringTokenizer(st1.nextToken(), "=");
                d2 = Double.parseDouble(st2.nextToken());
            }
            text.setText(text.getText() + " " + (d1 * d2));
        } else if (text.getText().contains("/")) {//做除法运算
            StringTokenizer st1 = new StringTokenizer(text.getText(), "/");
            d1 = Double.parseDouble(st1.nextToken());
            while (st1.hasMoreTokens()) {
                StringTokenizer st2 = new StringTokenizer(st1.nextToken(), "=");
                d2 = Double.parseDouble(st2.nextToken());
            }
            text.setText(text.getText() + " " + (d1 / d2));    //将运算结果放到text区域				}			}
        }
    }
    void sqrt( ){
        String str=new String(text.getText());
        double a=Double.parseDouble(str);
        System.out.println(Math.sqrt(a));
        text.setText(String.valueOf(Math.sqrt(a)));
    }
    void daoShu(){
        String str1=new String(text.getText());
        double b=Double.parseDouble(str1);
        System.out.println(1.0/b);
        text.setText(String.valueOf(1.0/b));
    }
    void prencent(){
        String str3=new String(text.getText());
        double c=Double.parseDouble(str3);
        System.out.println(c*0.01);
        text.setText(String.valueOf(c*0.01));
    }
}



